package com.nuanshui.heatedloan.credit.service;

import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.framework.security.DESC;
import com.nuanshui.heatedloan.constant.DubboCacheRegion;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.credit.dal.mapper.MerInfoMapper;
import com.nuanshui.heatedloan.credit.facade.MerInfoService;
import com.nuanshui.heatedloan.credit.form.MerInfoForm;
import com.nuanshui.heatedloan.credit.vo.MerInfoVO;
import com.nuanshui.heatedloan.dubbo.extension.cache.manager.DubboCacheManager;
import com.nuanshui.heatedloan.pageable.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zss
 * @date 2017/8/1 15:57
 * @description
 * @Since
 */
@Service(ServiceConstant.MERINFOSERVICE)
@Transactional(readOnly=true,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
public class MerInfoServiceImpl implements MerInfoService {

    @Autowired
    private MerInfoMapper merInfoMapper;

    @Autowired
    private DubboCacheManager dubboCacheManager;

    @Override
    public Pager<MerInfoVO> loadMerInfo(Pager<MerInfoVO> pager) {
        List<MerInfoVO> list = (List<MerInfoVO>)dubboCacheManager.get(DubboCacheRegion.CACHE_CONSTANT_MER_INFO_LIST,"#key");
        if (list == null){
            list = merInfoMapper.loadMerInfo(pager);
            dubboCacheManager.put(DubboCacheRegion.CACHE_CONSTANT_MER_INFO_LIST,list,"#key");
        }
        pager.setData(list);
        return pager;
    }

    @Override
    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public int saveMerInfo(MerInfoForm merInfoForm) {
        dubboCacheManager.evictCache(DubboCacheRegion.CACHE_CONSTANT_MER_INFO_LIST,"#key");
        merInfoForm.setPfxPassword(DESC.encrypt3DES(merInfoForm.getPfxPassword()));
        merInfoForm.setUserPassword(DESC.encrypt3DES(merInfoForm.getUserPassword()));
        return merInfoMapper.saveMerInfo(merInfoForm);
    }

    @Override
    public MerInfoVO loadMerInfoByMerChantId(String merChantId) {
        MerInfoVO vo =merInfoMapper.loadMerInfoByMerChantId(merChantId);
        if(vo != null ){
            vo.setPfxPassword(DESC.decrypt3DES(vo.getPfxPassword()));
            vo.setUserPassword(DESC.decrypt3DES(vo.getUserPassword()));
        }
        return vo;
    }

    @Override
    public MerInfoVO loadDefaultMerInfo() {
        MerInfoVO vo =merInfoMapper.loadDefaultMerInfo();
        if(vo != null ){
            vo.setPfxPassword(DESC.decrypt3DES(vo.getPfxPassword()));
            vo.setUserPassword(DESC.decrypt3DES(vo.getUserPassword()));
        }
        return vo;
    }

    @Override
    public int updateDefaultMerInfo(String id, int defaultMer) {
        dubboCacheManager.evictCache(DubboCacheRegion.CACHE_CONSTANT_MER_INFO_LIST,"#key");
        return merInfoMapper.updateDefaultMerInfo(id,defaultMer);
    }

}
